﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AccManagerKw.Common;
using Model;
using Model.Interfaces;
using Utilities;

namespace AccManagerKw.Masters
{
    public partial class FrmCityMaster : FrmBaseMaster, ICrud
    {
        private ClsCommonFn _oCommFn = new ClsCommonFn();
        private CityMaster _row = new CityMaster();
        public FrmCityMaster()
        {
            InitializeComponent();

        }
        private void FrmCityMaster_Shown(object sender, EventArgs e)
        {
            try
            {
                var context = new AccManagerEntities();
                lucStates.Properties.DataSource = context.StateMasters.ToList();
                lucStates.Properties.DisplayMember = "StateName";
                lucStates.Properties.ValueMember = "StateId";

                _oCommFn.GroupControl_Setting(gclMain);
                _oCommFn.GroupControl_Setting(gclOpen);
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
            }
        }


        public bool OperationSave()
        {
            try
            {
                if (IsValid())
                {
                    if (Mode == ClsGlobalVarz.Mode.Add && _oCommFn.ValidateCrud(Mode))
                    {
                        OperationCity();
                        _oCommFn.MsgBoxCrud(Mode);
                        return true;
                    }
                    else if (Mode == ClsGlobalVarz.Mode.Edit && _oCommFn.ValidateCrud(Mode))
                    {
                        OperationCity();
                        _oCommFn.MsgBoxCrud(Mode);
                        return true;
                    }
                    else if (Mode != ClsGlobalVarz.Mode.Add && Mode != ClsGlobalVarz.Mode.Edit)
                    {
                        _oCommFn.WarningMsg("Check Mode");
                        return false;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }

        private void OperationCity()
        {
            using (var context = new AccManagerEntities())
            {
                var oCm = new CityMaster();
                if (Mode == ClsGlobalVarz.Mode.Add)
                {
                    oCm.CityId = context.CityMasters.ToList().Count > 0
                        ? context.CityMasters.Max(x => x.CityId) + 1 : 1;
                }
                else if (Mode == ClsGlobalVarz.Mode.Edit)
                {
                    oCm = context.CityMasters.Single(x => x.CityId == _row.CityId);
                }

                oCm.CityCode = txtCityCode.Text;
                oCm.CityName = txtCityName.Text;
                oCm.PostalCode = txtPostalCode.Text;
                oCm.StateId = lucStates.Value;
                if (Mode == ClsGlobalVarz.Mode.Add)
                    context.CityMasters.Add(oCm);
                context.SaveChanges();
            }
        }

        public void OperationOpen()
        {
            try
            {
                var context = new AccManagerEntities();
                gcCities.DataSource = context.CityMasters.ToList();
                gclOpen.BringToFront();
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
            }
        }
        private void gvCities_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                _row = (CityMaster)gvCities.GetFocusedRow();
                txtCityCode.Text = _row.CityCode;
                txtCityName.Text = _row.CityName;
                txtPostalCode.Text = _row.PostalCode;
                lucStates.EditValue = _row.StateId;

                gclMain.BringToFront();
                _oCommFn.DisableControls(gclMain, true);
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
            }
        }


        public void OperationEdit()
        {
            try
            {
                if (_oCommFn.ValidateOpenRecord(_row.CityId))
                {
                    _oCommFn.DisableControls(gclMain, false);
                    SetMode(ClsGlobalVarz.Mode.Edit);
                }
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
            }
        }
        public bool OperationDelete()
        {
            try
            {
                if (_oCommFn.ValidateOpenRecord(_row.CityId))
                {
                    SetMode(ClsGlobalVarz.Mode.Delete);
                    if (_oCommFn.ValidateCrud(Mode))
                    {
                        using (var context = new AccManagerEntities())
                        {
                            var oCm = context.CityMasters.
                            Single(x => x.CityId == _row.CityId);
                            context.CityMasters.
                            Remove(oCm);
                            context.SaveChanges();
                            _oCommFn.MsgBoxCrud(Mode);
                        }
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }


        public void ResetData()
        {
            try
            {
                _oCommFn.ClearFields(this);
                _oCommFn.DisableControls(gclMain, false);
                gclMain.BringToFront();
                SetMode(ClsGlobalVarz.Mode.Add);
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
            }
        }
        public bool IsValid()
        {
            try
            {
                ccDxValidationProvider1.Validate();
                return ccDxValidationProvider1.GetInvalidControls().Count <= 0;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }


    }
}
